#include "BitmapFont.h"
#include "ReportError.h"

BitmapFont::BitmapFont()
{
	m_cellsX = 0;
	m_cellsY = 0;

}

bool BitmapFont::Load(const std::string& fileName, int xCells, int yCells)
{
	if(!m_tex.Load(fileName,true))
	{
		ReportError("Could not load BitmapFont texture file");
	}
	m_cellsX = xCells;
	m_cellsY = yCells;
	m_cellH = 1.0f / (float)m_cellsY;
	m_cellW = 1.0f / (float)m_cellsX;

	return true;
}

void BitmapFont::Draw(const std::string& text)
{
	float x = 0;
	glBegin(GL_QUADS);
	for (unsigned int i = 0; i < text.size(); i++)
	{
		char c = text[i];
		int cellNum = (int)(c - ' ');

		float u = (cellNum % m_cellsX) * m_cellH;
		float v = (cellNum / m_cellsX) * m_cellH;

		// Top left
		glTexCoord2f(u,v);
		glVertex2f(x,0);

		glTexCoord2f(u + m_cellW,v);
		glVertex2f( x, m_cellH);

		glTexCoord2f(u + m_cellW, v + m_cellH);
		glVertex2f(x + m_cellW, m_cellH);

		glTexCoord2f(u, v + m_cellH);
		glVertex2f(x + m_cellW, 0);

		//Change to vary size
		glTranslatef(0.2f, 0, 0);
	}
	glEnd();
}